package com.winz.cultureapp.activity;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.terry.zxing.zxing.android.CaptureActivity;
import com.winz.cultureapp.R;
import com.winz.cultureapp.adapter.vp.NewsVpAdapter;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.ActivityBookDetailBean;
import com.winz.cultureapp.entity.AdminBookDetailBean;
import com.winz.cultureapp.fragment.AdminActivityFragment;
import com.winz.cultureapp.fragment.AdminStadiumFragment;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.AdminBookDetailReq;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;
import com.winz.cultureapp.utils.SharePrefsUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 超管入口
 */
public class AdminActivity extends BaseAppCompatActivity {
    private final static int REQUEST_CODE_SCAN = 0X000001;
    private static final String DECODED_CONTENT_KEY = "codedContent";
    @BindView(R.id.tv_top_title)
    TextView mTv_top_title;
    @BindView(R.id.iv_right)
    ImageView mIv_riht;
    @BindView(R.id.rl_right)
    RelativeLayout mRl_right;
    @BindView(R.id.iv_other)
    ImageView mIv_date;
    @BindView(R.id.rl_other)
    RelativeLayout mRl_date;

    @BindView(R.id.stb_nav)
    SlidingTabLayout mStb_nav;
    @BindView(R.id.vp_list)
    ViewPager mVp_list;

    private String[] titles = {"场馆预定","活动预定"};
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected int contentView() {
        return R.layout.activity_admin;
    }

    @Override
    protected void initViews() {
        setStatusBar(getResources().getColor(R.color.base_color));
        mTv_top_title.setText(getString(R.string.admin_top_title));

        mIv_riht.setImageResource(R.drawable.icon_scan_w);
        mIv_riht.setVisibility(View.VISIBLE);

        fragments.add(AdminStadiumFragment.getInstance());
        fragments.add(AdminActivityFragment.getInstance());
        NewsVpAdapter mAdapter = new NewsVpAdapter(getSupportFragmentManager(),fragments, titles);
        mVp_list.setAdapter(mAdapter);

        mStb_nav.setViewPager(mVp_list,titles);
    }

    @OnClick({R.id.fl_back, R.id.rl_right, R.id.rl_other})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_back:
                finish();
                break;
            case R.id.rl_right:
                Intent intent = new Intent(this, CaptureActivity.class);
                startActivityForResult(intent, REQUEST_CODE_SCAN);
                break;
            case R.id.rl_other:
                break;
        }
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 扫描二维码/条码回传
        if (requestCode == REQUEST_CODE_SCAN && resultCode == RESULT_OK) {
            if (data != null) {
                String content = data.getStringExtra(DECODED_CONTENT_KEY);

                String flag = content.substring(content.indexOf("flag=")+5,content.indexOf("flag=")+6);
                String value = content.substring(content.lastIndexOf("str=")+4,content.length());
                switch (flag){
                    case "0":
                        // 活动订单详情
                        checkActivity(value);
                        break;
                    case "1":
                        // 场馆订单详情
                        checkStadium(value);
                        break;
                    default:
                        showMsg("未知类型");

                }
            }
        }
    }


    private void checkActivity(String randomstr) {
        showProgressDialog();
        HttpUitls.onPostByToken("admin_user_activity_book_detail", SharePrefsUtil.getInstance().getString(Constants.USER_INFO.TOKEN), new AdminBookDetailReq(randomstr), new OnResponeListener<ActivityBookDetailBean>() {
            @Override
            public void onSuccess(ActivityBookDetailBean bean) {
                Logger.d("admin_user_activity_book_detail==>>", JsonUtil.getJsonFromObj(bean));
                if (bean.code == 0) {
                    if (bean.data.useStatus == 0){
                        // 未使用
                        Bundle bundle = new Bundle();
                        bundle.putSerializable(Constants.KEY.ADMIN.DETAIL,bean.data);
                        bundle.putString(Constants.KEY.ADMIN.RANDOMSTR,randomstr);
                        startActivity(AdminActivityOrderActivity.class,false,bundle);
                    }else {
                        showMsg(bean.data.useStatus==1?"已使用":"已过期");
                    }

                }else{
                    showMsg(bean.msg);
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                Logger.d("admin_user_activity_book_detail==>>", error);
                dismissProgressDialog();
            }
        });
    }

    private void checkStadium(String randomstr) {
        showProgressDialog();
        HttpUitls.onPostByToken("admin_user_unit_book_detail", SharePrefsUtil.getInstance().getString(Constants.USER_INFO.TOKEN), new AdminBookDetailReq(randomstr), new OnResponeListener<AdminBookDetailBean>() {
            @Override
            public void onSuccess(AdminBookDetailBean bean) {
                Logger.d("admin_user_unit_book_detail==>>", JsonUtil.getJsonFromObj(bean));
                if (bean.code == 0) {
                    if (bean.data.useStatus == 0){
                        // 未使用
                        Bundle bundle = new Bundle();
                        bundle.putSerializable(Constants.KEY.ADMIN.DETAIL,bean.data);
                        bundle.putString(Constants.KEY.ADMIN.RANDOMSTR,randomstr);
                        startActivity(AdminStadiumOrderActivity.class,false,bundle);
                    }else {
                        showMsg(bean.data.useStatus==1?"已使用":"已过期");
                    }

                }else{
                    showMsg(bean.msg);
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                Logger.d("admin_user_unit_book_detail==>>", error);
                dismissProgressDialog();
            }
        });
    }

}
